package com.netbao.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.netbao.model.PageBean;
import com.netbao.model.PageResult;
import com.netbao.model.Result;
import com.netbao.sys.mapper.EnterpriseMapper;
import com.netbao.sys.mapper.RolerResMapper;
import com.netbao.sys.mapper.UserMapper;
import com.netbao.sys.model.*;
import com.netbao.sys.service.EnterpriseService;
import com.netbao.sys.utils.ShiroUtils;
import com.netbao.sys.vomapper.RoleUserVoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class EnterpriseServiceImpl implements EnterpriseService {
    @Resource
    private EnterpriseMapper enterpriseMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private RoleUserVoMapper roleUserVoMapper;
    @Resource
    private RolerResMapper RolerResMapper;
    @Override
    public PageResult list(PageBean pageBean, String cName) {
        EnterpriseExample example=new EnterpriseExample();
        EnterpriseExample.Criteria criteria = example.createCriteria();
        criteria.andIsdelEqualTo(false);
        if(!StringUtils.isEmpty(cName)){
            criteria.andCnameLike("%"+cName+"%");
        }
        PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        PageInfo<Enterprise> enterprisePageInfo = new PageInfo<>(enterpriseMapper.selectByExample(example));
        return new PageResult(enterprisePageInfo.getTotal(),enterprisePageInfo.getList());
    }

    @Override
    @Transactional
    public Result save(Enterprise enterprise) {
        Date date = new Date();
        if(enterprise.getEid()==null){
            enterprise.setCreatetime(date);
            enterpriseMapper.insertSelective(enterprise);
            User user = new User();
            user.setAccount("admin"+enterprise.getEid());
            user.setNickname(enterprise.getCname());
            user.setCname("管理员");
            user.setPassword(ShiroUtils.sha256("123","admin"+enterprise.getEid()));
            user.setEid((Integer) enterprise.getEid().intValue());
            user.setIsadmin(true);
            user.setRoles("管理员");
            user.setRemark("管理员");
            userMapper.insertSelective(user);
            roleUserVoMapper.saveRole("2",user.getId().longValue());
        }else {
//            System.out.println("XXXXXXXXXXXXXXXXXXX"+enterprise.getIscheck());
            if(enterprise.getIscheck()!=null){
                enterprise.setChecktime(date);
            }
            enterprise.setLasttime(date);
//            enterpriseMapper.updateByPrimaryKeySelective(enterprise);
//            EnterpriseExample example = new EnterpriseExample();
//            example.createCriteria().andIscheckEqualTo(false);
            enterpriseMapper.updateByPrimaryKeySelective(enterprise);
        }
        return Result.ok();
    }

    @Override
    @Transactional
    public Result delete(Long eid) {
        Enterprise enterprise = new Enterprise();
        enterprise.setEid(eid);
        enterprise.setIsdel(true);
        enterpriseMapper.updateByPrimaryKeySelective(enterprise);//删除企业
        User user = new User();
        user.setIsdel(true);
        UserExample userExample=new UserExample();
        userExample.createCriteria().andEidEqualTo(eid.intValue());
        List<User> users = userMapper.selectByExample(userExample);//删除user
        for (User user1 : users) {
            userMapper.deleteByPrimaryKey(user1.getId());
        }
        //删除角色用户表
        UserRoleExample userRoleExample = new UserRoleExample();
        //删除角色资源表
        return Result.ok();
    }
}
